package com.cyw.note.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author: Chenyw
 * @Date: 2022/08/26/15:20
 */
@Component
public class EmailUtil {

    @Value("${mail.fromMail.sender}")
    private String sender;

    @Autowired
    private JavaMailSender javaMailSender;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 发送普通文本邮件
     */
    @Async
    public String sendMail(String receiver,String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(receiver);
        message.setSubject("cyw");
        message.setText(content);
        try {
            javaMailSender.send(message);
            logger.info("简单邮件已经发送。");
            return "success";
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常！", e);
            return "error";
        }
    }
}
